﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace IndexMarkSilverlightClient.IndexMarkServiceReference
{
    public partial class Gradation
    {
        public bool isCorrect = false;

        public override string ToString()
        {
            if (this.Volume != null)
            {
                return this.Name + " - " + this.Volume + " " + Unit;
            }
            else
            {
                return this.Group + " - " + this.Name;
            }
        }

        /// <summary>
        /// показывает, что градации не имеет потомков
        /// </summary>
        public bool NotHasChilds
        {
            get
            {
                try
                {
                    return this.Childs.Count == 0;
                }
                catch
                {
                    return this.Childs == null;
                }
            }
        }

        public string FullName
        {
            get
            {
                return this.Order + " - " + this.Name;
            }
        }

        public override bool Equals(object obj)
        {
            try
            {
                return this.ID == (obj as Gradation).ID;
            }
            catch
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return 0;
        }

        public Gradation FindInChild(Gradation Child)
        {
            Gradation Result = null;

            if (this.ID == Child.ID)
            {
                return this;
            }
            else
            {
                foreach (Gradation G in Childs)
                {
                    Result = G.FindInChild(Child);
                    if (Result != null)
                    {
                        return Result;
                    }
                }
            }

            return Result;
        }

        private Gradation Find(Gradation Child)
        {
            if (this.ID == Child.ID)
            {
                return this;
            }
            
            foreach (Gradation G in Childs)
            {
                if (Child.ID == G.ID)
                {
                    return G;
                }
            }

            foreach (Gradation G in Childs)
            {
                return G.Find(Child);
            }

            return null;
        }

        public bool IsCorrect
        {
            get
            {
                if (!IsNecessary)
                {
                    return true;
                }
                else
                {
                    if (this.Childs.Count == 0 && this.Volume > 0) return true;

                    isCorrect = false;
                    bool res = IsSelectedRecursive(Childs);
                    return res;
                    
                    //foreach (DTOBase DTO in Childs)
                    //{
                    //    if ((DTO as Gradation).IsSelected)
                    //        return true;
                    //    else
                    //    {
                    //        bool res = IsSelectedRecursive(Childs);
                    //        return res;
                    //    }
                    //}
                    //foreach (DTOBase DTO in Childs)
                    //{
                    //    if ((DTO as Gradation).IsSelected) return true;
                    //}

                    //return false;
                }

            }
        }

        public bool IsSelectedRecursive(ObservableCollection<DTOBase> childs)
        {
            foreach (DTOBase DTO in childs)
            {
                if ((DTO as Gradation).IsSelected)
                {
                    isCorrect = true;
                    return true;
                }
                else
                {
                    if (isCorrect == false)
                        IsSelectedRecursive((DTO as Gradation).Childs);
                    else
                        return true;
                }
            }

            return isCorrect;
        }

        public void PropertyChangedInit(String PropertyName)
        {
            this.RaisePropertyChanged(PropertyName);
        }
    }
}
